package com.shujia.jichu

import java.io.{BufferedReader, BufferedWriter, FileReader, FileWriter}
import scala.io.{BufferedSource, Source}

object Demo3IO {
  def main(args: Array[String]): Unit = {
    /**
     * 读取文件
     *   1、使用java中的IO类来读取数据
     *   2、使用scala语法读取数据文件
     */
    val br = new BufferedReader(new FileReader("scala/data/words.txt"))
    var line: String = br.readLine()
    while (line!=null){
      println(line)
      line = br.readLine()
    }

    br.close() //释放资源

    println("="*50)
    val bs: BufferedSource = Source.fromFile("scala/data/words.txt")
    val lines: Iterator[String] = bs.getLines()
    for (elem <- lines) {
      println(elem)
    }
    bs.close()

    //使用java的方式写数据
    val bw = new BufferedWriter(new FileWriter("scala/data/out1.txt"))
    bw.write("hello world1")
    bw.newLine()
    bw.write("hello world2")
    bw.close()

    //scala中原生没有写数据的方式

  }

}
